<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="作业任务ID" prop="jobId">
                        <el-input v-model="dataForm.jobId" placeholder="作业任务ID"></el-input>
                </el-form-item>
                <el-form-item label="作业任务名称" prop="jobName">
                        <el-input v-model="dataForm.jobName" placeholder="作业任务名称"></el-input>
                </el-form-item>
                <el-form-item label="作业开始时间" prop="startTime">
                        <el-date-picker
                                v-model="dataForm.startTime"
                                type="datetime"
                                placeholder="作业开始时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="作业结束时间" prop="endTime">
                        <el-date-picker
                                v-model="dataForm.endTime"
                                type="datetime"
                                placeholder="作业结束时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="作业进度百分比" prop="progress">
                        <el-input v-model="dataForm.progress" placeholder="作业进度百分比"></el-input>
                </el-form-item>
                <el-form-item label="船舶ID" prop="vesselId">
                        <el-input v-model="dataForm.vesselId" placeholder="船舶ID"></el-input>
                </el-form-item>
                <el-form-item label="舱位位置" prop="holdPosition">
                        <el-input v-model="dataForm.holdPosition" placeholder="舱位位置"></el-input>
                </el-form-item>
                <el-form-item label="作业机械ID" prop="machineryId">
                        <el-input v-model="dataForm.machineryId" placeholder="作业机械ID"></el-input>
                </el-form-item>
                <el-form-item label="最后更新时间" prop="lastUpdate">
                        <el-date-picker
                                v-model="dataForm.lastUpdate"
                                type="datetime"
                                placeholder="最后更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="作业状态（进行中、已完成、异常等）" prop="status">
                        <el-input v-model="dataForm.status" placeholder="作业状态（进行中、已完成、异常等）"></el-input>
                </el-form-item>
                <el-form-item label="备注信息" prop="remark">
                        <el-input v-model="dataForm.remark" placeholder="备注信息"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            id: null,
            jobId: null,
            jobName: null,
            startTime: null,
            endTime: null,
            progress: null,
            vesselId: null,
            holdPosition: null,
            machineryId: null,
            lastUpdate: null,
            status: null,
            remark: null
          },
          dataRule: {
            id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
            ],
            jobId: [
                        { required: true, message: '作业任务ID不能为空', trigger: 'blur' }
            ],
            jobName: [
                        { required: true, message: '作业任务名称不能为空', trigger: 'blur' }
            ],
            startTime: [
                        { required: true, message: '作业开始时间不能为空', trigger: 'blur' }
            ],
            endTime: [
                        { required: true, message: '作业结束时间不能为空', trigger: 'blur' }
            ],
            progress: [
                        { required: true, message: '作业进度百分比不能为空', trigger: 'blur' }
            ],
            vesselId: [
                        { required: true, message: '船舶ID不能为空', trigger: 'blur' }
            ],
            holdPosition: [
                        { required: true, message: '舱位位置不能为空', trigger: 'blur' }
            ],
            machineryId: [
                        { required: true, message: '作业机械ID不能为空', trigger: 'blur' }
            ],
            lastUpdate: [
                        { required: true, message: '最后更新时间不能为空', trigger: 'blur' }
            ],
            status: [
                        { required: true, message: '作业状态（进行中、已完成、异常等）不能为空', trigger: 'blur' }
            ],
            remark: [
                        { required: true, message: '备注信息不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.orderId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.orderId = id || 0
            if (this.dataForm.orderId) {
              this.$http({
                url: this.$http.adornUrl(`/app/AppJobProgress/info/\\$\\{this.dataForm.orderId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appjobprogress
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/AppJobProgress/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>